###### Title ##########################
## Insecurity and Support for Female Leadership in Conflict States: 
## Evidence from Afghanistan by Jasmine Bhatia and
## Steve L. Monroe.
## Replication Command File: Conjoint Experiment (Balance Tables)  
## August 2023
##

# This file for creating the balance tables of the conjoint survey experiment
# presented in the Supplementary Information


### Table of Contents  ##################################
## 1. Load packages, data and clean data 
## 2. Clean Variables
## 3. Create Balance Tables


## Note: All code run using R 4.0.3 GUI 1.73 Catalina build (7892)

######## 1. Load Necessary Packages and Data ##############################

library(cjoint)
library(dplyr)
library(tidyr)
library(xtable)
library(tidyverse)
library(stats)
library(survey)
library(Rcpp)
library(estimatr)
library(knitr)
library(DT)
library(cli)
library(devtools)
devtools::install_github("m-freitag/cjpowR")
library(cjpowR)
library(stargazer)
library(MASS)
library(mlogit)
library("nnet")
library("scales")
library(here)

# set working directory and load dataset

getwd()


data <- read.csv("AfghanDataInscCtrlOnly_Clean.csv")

##### 2. Clean Variables #################################
##    Subset and clean variables


# subset data for balance table

balance <- data[, c("ID","Res_Gender", "Literacy", "Employ_Unem",
                    "TravelEase", "HH_Head_Edu", "Res_Education",
               "Province", "TreatInsc" , "ControlGrp", "Res_Age", "Res_Ethnicity",
               "Behav_Peace", "Att_Afg_Mil", "Voted_Pres", "IntForces",
               "TrustProvGov", "TrustCentGov", "Att_Strong_Rel",
               "Att_Gov_Exp", "Att_Muj", "Behav_law_punish", "Behav_ethnic")]

# keep only unique observations (don't want profile observations)

balance <- unique(balance) 

# clean and make balance table variables

balance$edu_numeric <- ifelse(balance$Res_Education == 1, 0,
                      ifelse(balance$Res_Education == 8, 0,
                       ifelse(balance$Res_Education == 2, 1,
                       ifelse(balance$Res_Education == 3, 2,      
                       ifelse(balance$Res_Education == 4, 3,     
                       ifelse(balance$Res_Education == 5, 4,
                       ifelse(balance$Res_Education == 6, 5, 
                        NA)))))))
                              
# unemployed = 1
balance$Employ_Unem[is.na(balance$Employ_Unem)] <- 0 # set NAs to 0


# female: Res_Gender = female is 2

balance$female <- ifelse(balance$Res_Gender == 2, 1, 0)


# ethnicity variables

balance$pashtun <- ifelse(balance$Res_Ethnicity == 1, 1, 0)
balance$tajik <- ifelse(balance$Res_Ethnicity == 2, 1, 0)
balance$uzbek <- ifelse(balance$Res_Ethnicity == 3, 1, 0)
balance$hazara <- ifelse(balance$Res_Ethnicity == 5, 1, 0)
balance$other <- ifelse(balance$Res_Ethnicity == 4, 1,
                 ifelse(balance$Res_Ethnicity == 6, 1,      
                  ifelse(balance$Res_Ethnicity == 7, 1,       
                   0)))

# Voted last presidential election

balance$voted <- ifelse(balance$Voted_Pres == 1, 1, 0)


# Create attitudes toward security variable


balance$Behav_Peace_new <- case_when(
  balance$Behav_Peace == 1 ~ 6,
  balance$Behav_Peace  == 2 ~ 5, 
  balance$Behav_Peace == 3 ~ 4, 
  balance$Behav_Peace == 4 ~ 3,
  balance$Behav_Peace == 5 ~ 2,
  balance$Behav_Peace== 6 ~ 1,
  TRUE ~ NA_real_
)


balance$Behav_law_punish_new <- case_when(
  balance$Behav_law_punish == 1 ~ 6,
  balance$Behav_law_punish  == 2 ~ 5, 
  balance$Behav_law_punish == 3 ~ 4, 
  balance$Behav_law_punish == 4 ~ 3,
  balance$Behav_law_punish== 5 ~ 2,
  balance$Behav_law_punish == 6 ~ 1,
  TRUE ~ NA_real_
)



balance$Behav_ethnic_new <- case_when(
  balance$Behav_ethnic == 1 ~ 6,
  balance$Behav_ethnic  == 2 ~ 5, 
  balance$Behav_ethnic == 3 ~ 4, 
  balance$Behav_ethnic == 4 ~ 3,
  balance$Behav_ethnic == 5 ~ 2,
  balance$Behav_ethnic == 6 ~ 1,
  TRUE ~ NA_real_
)


balance$Religious_new <- case_when(
  balance$Att_Strong_Rel == 1 ~ 6,
  balance$Att_Strong_Rel  == 2 ~ 5, 
  balance$Att_Strong_Rel== 3 ~ 4, 
  balance$Att_Strong_Rel == 4 ~ 3,
  balance$Att_Strong_Rel == 5 ~ 2,
  balance$Att_Strong_Rel== 6 ~ 1,
  TRUE ~ NA_real_
)


balance$Gov_Exp_new <- case_when(
  balance$Att_Gov_Exp == 1 ~ 6,
  balance$Att_Gov_Exp  == 2 ~ 5, 
  balance$Att_Gov_Exp == 3 ~ 4, 
  balance$Att_Gov_Exp == 4 ~ 3,
  balance$Att_Gov_Exp == 5 ~ 2,
  balance$Att_Gov_Exp == 6 ~ 1,
  TRUE ~ NA_real_
)


balance$Afg_Mil_new <- case_when(
  balance$Att_Afg_Mil == 1 ~ 6,
  balance$Att_Afg_Mil  == 2 ~ 5, 
  balance$Att_Afg_Mil == 3 ~ 4, 
  balance$Att_Afg_Mil == 4 ~ 3,
  balance$Att_Afg_Mil == 5 ~ 2,
  balance$Att_Afg_Mil == 6 ~ 1,
  TRUE ~ NA_real_
)


balance$Afg_Muj_new <- case_when(
  balance$Att_Muj == 1 ~ 6,
  balance$Att_Muj  == 2 ~ 5, 
  balance$Att_Muj == 3 ~ 4, 
  balance$Att_Muj == 4 ~ 3,
  balance$Att_Muj == 5 ~ 2,
  balance$Att_Muj == 6 ~ 1,
  TRUE ~ NA_real_
)



## Attitude questions:
# Support for international forces

balance$IntForces_new <- case_when(
  balance$IntForces == 1 ~ 5,
  balance$IntForces  == 2 ~ 4, 
  balance$IntForces == 3 ~ 3, 
  balance$IntForces == 4 ~ 2,
  balance$IntForces == 5 ~ 1,
  TRUE ~ NA_real_
)


# Trust in Central Gov

balance$TrustCentGov_new <- case_when(
  balance$TrustCentGov == 1 ~ 5,
  balance$TrustCentGov  == 2 ~ 4, 
  balance$TrustCentGov == 3 ~ 3, 
  balance$TrustCentGov == 4 ~ 2,
  balance$TrustCentGov == 5 ~ 1,
  TRUE ~ NA_real_
)


# Trust in provincial gov

balance$TrustProvGov_new <- case_when(
  balance$TrustProvGov == 1 ~ 5,
  balance$TrustProvGov  == 2 ~ 4, 
  balance$TrustProvGov == 3 ~ 3, 
  balance$TrustProvGov == 4 ~ 2,
  balance$TrustProvGov == 5 ~ 1,
  TRUE ~ NA_real_
)




#create province dummy variables

balance$balkh <- ifelse(balance$Province == "Balkh", 1, 0)

balance$kundoz <- ifelse(balance$Province == "Kundoz", 1, 0)

balance$sarepull <- ifelse(balance$Province == "Sar-e-pull", 1, 0)


# Create Manipulation Check Variable for Treated Respondents
# True or one is the correct answer. It looks like only treated
# respondents received a manipulation check. 

balance$manipulation <- ifelse(balance$TreatInsc == 1, 1,
                               0)

# Create Treatment Variable: Set NAs to 0, then equal 1 if 
# insecurity prime is received. This is measured as the TreatInsc
# variable having a value greater than 0

balance$TreatInsc[is.na(balance$TreatInsc)] <- 0

# Make Variable a Dummy

balance$TreatInsc <- ifelse(balance$TreatInsc > 0, 1, 0)



### 3. Create Balance Tables ######

## SI Table 1: Neutral Text vs. Insecurity Prime (SI 2.1)

si_table1 <- balance %>%
                 group_by(TreatInsc) %>%
                 summarize(
                 avg_behave_peace = mean(Behav_Peace_new),
                 sd_behave_peace = sd(Behav_Peace_new),   
                 avg_female = mean(female),
                 sd_female = sd(female),
                 avg_edu = mean(edu_numeric),
                 sd_edu = sd(edu_numeric), 
                 avg_unemployed = mean(Employ_Unem),
                 sd_unemployed = sd(Employ_Unem),
                 avg_voted = mean(voted),
                 sd_voted = sd(voted),
                 avg_pashtun = mean(pashtun),
                 sd_pashtun  = sd(pashtun),
                 avg_uzbek = mean(uzbek),
                 sd_uzbek  = sd(uzbek),
                 avg_tajik = mean(tajik),
                 sd_takjik  = sd(tajik),
                 avg_hazara = mean(hazara),
                 sd_hazara  = sd(hazara),
                 avg_other = mean(other),
                 sd_other  = sd(other),
                 avg_balkh = mean(balkh),
                 sd_balkh = sd(balkh),
                 avg_kundoz = mean(kundoz),
                 sd_kundoz = sd(kundoz),
                 avg_sarepull = mean(sarepull),
                 sd_sarepull = sd(sarepull),
                 count = n())


pvl1 <- balance %>%
              summarise(
              pval_behave_peace = t.test(Behav_Peace_new~ TreatInsc, 
                                           var.equal = TRUE)$p.value,
              pval_female = t.test(female ~ TreatInsc, 
                              var.equal = TRUE)$p.value,
              pval_edu_numeric = t.test(edu_numeric ~ TreatInsc,
                               var.equal = TRUE)$p.value,
              pval_unemployed = t.test(Employ_Unem ~ TreatInsc, 
                                   var.equal = TRUE)$p.value,
              pval_voted = t.test(voted ~ TreatInsc, 
                                       var.equal = TRUE)$p.value,
              pval_pashtun = t.test(pashtun ~ TreatInsc, 
                                    var.equal = TRUE)$p.value,
              pval_uzbek = t.test(uzbek ~ TreatInsc, 
                                    var.equal = TRUE)$p.value,
              pval_tajik = t.test(tajik ~ TreatInsc, 
                                  var.equal = TRUE)$p.value,
              pval_hazara = t.test(hazara ~ TreatInsc, 
                                  var.equal = TRUE)$p.value,
              pval_other = t.test(other ~ TreatInsc, 
                                   var.equal = TRUE)$p.value,            
              pval_balkh = t.test(balkh~ TreatInsc,
                              var.equal = TRUE)$p.value,
              pval_kundoz = t.test(kundoz~ TreatInsc,
                                  var.equal = TRUE)$p.value,
              pval_sarepull = t.test(sarepull~ TreatInsc,
                                   var.equal = TRUE)$p.value,
              )

control <- as.vector(t(si_table1[1,-c(1,28)]))
treatment <- as.vector(t(si_table1[2,-c(1,28)]))


pval_one <- as.vector(unlist(c(pvl1[1], NA, pvl1[2], NA, pvl1[3], NA, 
                                pvl1[4], NA, pvl1[5], NA, pvl1[6], NA,
                                pvl1[7], NA, pvl1[8], NA, pvl1[9], NA,
                                pvl1[10], NA, pvl1[11], NA, pvl1[12],
                                NA, pvl1[13], NA
                                )))


b_vars <- c(
  "Support Security", "",
  "Female", "",
  "Education", "",
  "Unemployed", "",
  "Voted", "",
  "Pashtun", "",
  "Uzbek", "",
  "Tajik", "",
  "Hazara", "",
  "Other", "",
  "Balkh", "",
  "Kundoz", "",
  "Sar-e-Pull", "",
  "Respondents")


counts <- si_table1$count

cb_tab <- data.frame(matrix(nrow = 27, ncol = 4))

cb_tab[,1] <- c(b_vars)
cb_tab[,2] <- c(control, counts[1])
cb_tab[,3] <- c(treatment, counts[2])
cb_tab[,4] <- c(pval_one, NA)


# Round all numbers to 3 digits
cb_tab <- cb_tab %>%
  purrr::modify_if(~is.numeric(.), ~round(., 3))


# Add descriptive row at top
si_tab1 <- data.frame(rbind(c("", "Mean \n (SD)", "Mean \n (SD)", 
                              "P Value"),
                            cb_tab))

# Add column names
names(si_tab1) <- c("", "Neutral Prime", "Insecurity Prime", "")

View(si_tab1) #  SI Balance Table 1 


## SI Table 2: Balance Table: Male vs. Female Respondents


si_table2 <- balance %>%
                  group_by(female) %>%
                  summarize(
                  avg_behave_peace = mean(Behav_Peace_new),
                  sd_behave_peace = sd(Behav_Peace_new),  
                  avg_edu_numeric = mean(edu_numeric),
                  sd_edu_numeric = sd(edu_numeric), 
                  avg_unemployed = mean(Employ_Unem),
                  sd_unemployed = sd(Employ_Unem),
                  avg_voted = mean(voted),
                  sd_voted = sd(voted),
                  avg_pashtun = mean(pashtun),
                  sd_pashtun  = sd(pashtun),
                  avg_uzbek = mean(uzbek),
                  sd_uzbek  = sd(uzbek),
                  avg_tajik = mean(tajik),
                  sd_takjik  = sd(tajik),
                  avg_hazara = mean(hazara),
                  sd_hazara  = sd(hazara),
                  avg_other = mean(other),
                  sd_other  = sd(other),
                  avg_balkh = mean(balkh),
                  sd_balkh = sd(balkh),
                  avg_kundoz = mean(kundoz),
                  sd_kundoz = sd(kundoz),
                  avg_sarepull = mean(sarepull),
                  sd_sarepull = sd(sarepull),
                  count = n())


pvl2 <- balance %>%
              summarise(
              pval_behave_peace = t.test(Behav_Peace_new~ female, 
                                           var.equal = TRUE)$p.value,
              pval_edu_numeric = t.test(edu_numeric ~ female,
                                var.equal = TRUE)$p.value,
              pval_unemployed = t.test(Employ_Unem ~ female, 
                             var.equal = TRUE)$p.value,
              pval_voted = t.test(voted ~ female, 
                             var.equal = TRUE)$p.value,
              pval_pashtun = t.test(pashtun ~ female, 
                          var.equal = TRUE)$p.value,
              pval_uzbek = t.test(uzbek ~ female, 
                        var.equal = TRUE)$p.value,
              pval_tajik = t.test(tajik ~ female, 
                        var.equal = TRUE)$p.value,
              pval_hazara = t.test(hazara ~ female, 
                         var.equal = TRUE)$p.value,
              pval_other = t.test(other ~ female, 
                        var.equal = TRUE)$p.value,  
              pval_balkh = t.test(balkh~ female,
                        var.equal = TRUE)$p.value,
              pval_kundoz = t.test(kundoz~ female,
                         var.equal = TRUE)$p.value,
              pval_sarepull = t.test(sarepull~ female,
                           var.equal = TRUE)$p.value,
              )

control <- as.vector(t(si_table2[1,-c(1,26)]))
treatment <- as.vector(t(si_table2[2,-c(1,26)]))


pval_two <- as.vector(unlist(c(pvl2[1], NA, pvl2[2], NA, pvl2[3], NA, 
                                pvl2[4], NA, pvl2[5], NA, pvl2[6], NA,
                                pvl2[7], NA, pvl2[8], NA, pvl2[9], NA,
                                pvl2[10], NA, pvl2[11], NA, pvl2[12],
                                NA)))


b_vars <- c(
          "Support Security", "",
          "Education", "",
          "Unemployed", "",
          "Voted", "",
          "Pashtun", "",
          "Uzbek", "",
          "Tajik", "",
          "Hazara", "",
          "Other", "",
          "Balkh", "",
          "Kundoz", "",
          "Sar-e-Pull", "",
          "Respondents")


counts <- si_table2$count

cb_tab <- data.frame(matrix(nrow = 25, ncol = 4))

cb_tab[,1] <- c(b_vars)
cb_tab[,2] <- c(control, counts[1])
cb_tab[,3] <- c(treatment, counts[2])
cb_tab[,4] <- c(pval_two, NA)


# Round all numbers to 3 digits
cb_tab <- cb_tab %>%
  purrr::modify_if(~is.numeric(.), ~round(., 3))


# Add descriptive row at top
si_tab2 <- data.frame(rbind(c("", "Mean \n (SD)", "Mean \n (SD)", 
                              "P Value"),
                            cb_tab))

# Add column names
names(si_tab2) <- c("", "Men", "Women", "")

View(si_tab2) # SI Table 2


### SI Table 3: Mechanism: Neutral Text vs Insecurity Prime (SI 2.2)

si_table3 <- balance %>%
             group_by(TreatInsc) %>%
  summarize(
    avg_behave_peace = mean(Behav_Peace_new),
    sd_behave_peace = sd(Behav_Peace_new),
    avg_military = mean(Afg_Mil_new),
    sd_military = sd(Afg_Mil_new),
    avg_muj = mean(Afg_Muj_new),
    sd_muj = sd(Afg_Muj_new),
    avg_religion = mean(Religious_new),
    sd_religion = sd(Religious_new),
    avg_punish = mean(Behav_law_punish_new),
    sd_punish = sd(Behav_law_punish_new),
    avg_TrustCentGovs = mean(TrustCentGov_new),
    sd_TrustCentGov = sd(TrustCentGov_new),
    avg_TrustProvGov = mean(TrustProvGov_new),
    sd_TrustProvGov = sd(TrustProvGov_new),
    avg_int = mean(IntForces_new),
    sd_int = sd(IntForces_new),
    count = n())


pvl3 <- balance %>%
  summarise(
    pval_behave_peace = t.test(Behav_Peace_new~ TreatInsc, 
                               var.equal = TRUE)$p.value,
    pval_military = t.test(Afg_Mil_new ~ TreatInsc,
                           var.equal = TRUE)$p.value,
    pval_muj = t.test(Afg_Muj_new ~ TreatInsc, 
                      var.equal = TRUE)$p.value,
    pval_religion = t.test(Religious_new~ TreatInsc, 
                           var.equal = TRUE)$p.value,
    pval_behave_punish = t.test(Behav_law_punish_new~ TreatInsc, 
                                var.equal = TRUE)$p.value,
    pval_TrustCentGov = t.test(TrustCentGov_new~ TreatInsc, 
                               var.equal = TRUE)$p.value,
    pval_TrustProvGov = t.test(TrustProvGov_new~ TreatInsc, 
                               var.equal = TRUE)$p.value,
    pval_int = t.test(IntForces_new~ TreatInsc, 
                      var.equal = TRUE)$p.value,
    
  )

control <- as.vector(t(si_table3[1,-c(1,18)]))
treatment <- as.vector(t(si_table3[2,-c(1,18)]))


pval_three <- as.vector(unlist(c(pvl3[1], NA, pvl3[2], NA, pvl3[3], NA, 
                                     pvl3[4], NA, pvl3[5], NA, pvl3[6], NA,
                                     pvl3[7], NA, pvl3[8], NA
)))


b_vars <- c(
  "Security Support", "",
  "Military Experience", "",
  "Mujahideen Experience", "",
  "Religiosity", "",
  "Punish Crime", "",
  "National Gov Support", "",
  "Prov Gov Support", "",
  "Int Support", "",
  "Respondents")


counts <- si_table3$count

cb_tab <- data.frame(matrix(nrow = 17, ncol = 4))

cb_tab[,1] <- c(b_vars)
cb_tab[,2] <- c(control, counts[1])
cb_tab[,3] <- c(treatment, counts[2])
cb_tab[,4] <- c(pval_three, NA)


# Round all numbers to 3 digits
cb_tab <- cb_tab %>%
  purrr::modify_if(~is.numeric(.), ~round(., 4))


# Add descriptive row at top
si_tab3 <- data.frame(rbind(c("", "Mean \n (SD)", "Mean \n (SD)", 
                                        "P Value"),
                                      cb_tab))

# Add column names
names(si_tab3) <- c("", "Neutral Prime", "Insecurity Prime", "")

View(si_tab3) # SI Table 3  


### SI Table 4: Mechanism: Neutral Text vs. Insecurity Prime (Female Respondents)


si_table4 <- balance[balance$female == 1,] %>%
  group_by(TreatInsc) %>%
  summarize(
    avg_behave_peace = mean(Behav_Peace_new),
    sd_behave_peace = sd(Behav_Peace_new),
    avg_military = mean(Afg_Mil_new),
    sd_military = sd(Afg_Mil_new),
    avg_muj = mean(Afg_Muj_new),
    sd_muj = sd(Afg_Muj_new),
    avg_religion = mean(Religious_new),
    sd_religion = sd(Religious_new),
    avg_punish = mean(Behav_law_punish_new),
    sd_punish = sd(Behav_law_punish_new),
    avg_TrustCentGovs = mean(TrustCentGov_new),
    sd_TrustCentGov = sd(TrustCentGov_new),
    avg_TrustProvGov = mean(TrustProvGov_new),
    sd_TrustProvGov = sd(TrustProvGov_new),
    avg_int = mean(IntForces_new),
    sd_int = sd(IntForces_new),
    count = n())


pvl4 <- balance[balance$female == 1,]  %>%
  summarise(
    pval_behave_peace = t.test(Behav_Peace_new~ TreatInsc, 
                               var.equal = TRUE)$p.value,
    pval_military = t.test(Afg_Mil_new ~ TreatInsc,
                           var.equal = TRUE)$p.value,
    pval_muj = t.test(Afg_Muj_new ~ TreatInsc, 
                      var.equal = TRUE)$p.value,
    pval_religion = t.test(Religious_new~ TreatInsc, 
                           var.equal = TRUE)$p.value,
    pval_behave_punish = t.test(Behav_law_punish_new~ TreatInsc, 
                                var.equal = TRUE)$p.value,
    pval_TrustCentGov = t.test(TrustCentGov_new~ TreatInsc, 
                               var.equal = TRUE)$p.value,
    pval_TrustProvGov = t.test(TrustProvGov_new~ TreatInsc, 
                               var.equal = TRUE)$p.value,
    pval_int = t.test(IntForces_new~ TreatInsc, 
                      var.equal = TRUE)$p.value,
  )

control <- as.vector(t(si_table4[1,-c(1,18)]))
treatment <- as.vector(t(si_table4[2,-c(1,18)]))


pval_four <- as.vector(unlist(c(pvl4[1], NA, pvl4[2], NA, pvl4[3], NA, 
                                     pvl4[4], NA, pvl4[5], NA, pvl4[6], NA,
                                     pvl4[7], NA, pvl4[8], NA
)))


b_vars <- c(
  "Security Support", "",
  "Military Experience", "",
  "Mujahideen Experience", "",
  "Religiosity", "",
  "Punish Crime", "",
  "National Gov Support", "",
  "Prov Gov Support", "",
  "Int Support", "",
  "Respondents")


counts <- si_table4$count

cb_tab <- data.frame(matrix(nrow = 17, ncol = 4))

cb_tab[,1] <- c(b_vars)
cb_tab[,2] <- c(control, counts[1])
cb_tab[,3] <- c(treatment, counts[2])
cb_tab[,4] <- c(pval_four, NA)


# Round all numbers to 3 digits
cb_tab <- cb_tab %>%
  purrr::modify_if(~is.numeric(.), ~round(., 4))


# Add descriptive row at top
si_tab4<- data.frame(rbind(c("", "Mean \n (SD)", "Mean \n (SD)", 
                                        "P Value"),
                                      cb_tab))

# Add column names
names(si_tab4) <- c("", "Neutral Prime", "Insecurity Prime", "")

View(si_tab4) # Balance Table 4



## SI Table 5: Mechanisms: Neutral Text vs. Insecurity Prime Female Respondents (SI 2.2)

si_table5 <- balance[balance$female == 0,] %>%
  group_by(TreatInsc) %>%
  summarize(
    avg_behave_peace = mean(Behav_Peace_new),
    sd_behave_peace = sd(Behav_Peace_new),
    avg_military = mean(Afg_Mil_new),
    sd_military = sd(Afg_Mil_new),
    avg_muj = mean(Afg_Muj_new),
    sd_muj = sd(Afg_Muj_new),
    avg_religion = mean(Religious_new),
    sd_religion = sd(Religious_new),
    avg_punish = mean(Behav_law_punish_new),
    sd_punish = sd(Behav_law_punish_new),
    avg_TrustCentGovs = mean(TrustCentGov_new),
    sd_TrustCentGov = sd(TrustCentGov_new),
    avg_TrustProvGov = mean(TrustProvGov_new),
    sd_TrustProvGov = sd(TrustProvGov_new),
    avg_int = mean(IntForces_new),
    sd_int = sd(IntForces_new),
    count = n())


pvl5 <- balance[balance$female == 0,]  %>%
  summarise(
    pval_behave_peace = t.test(Behav_Peace_new~ TreatInsc, 
                               var.equal = TRUE)$p.value,
    pval_military = t.test(Afg_Mil_new ~ TreatInsc,
                           var.equal = TRUE)$p.value,
    pval_muj = t.test(Afg_Muj_new ~ TreatInsc, 
                      var.equal = TRUE)$p.value,
    pval_religion = t.test(Religious_new~ TreatInsc, 
                           var.equal = TRUE)$p.value,
    pval_behave_punish = t.test(Behav_law_punish_new~ TreatInsc, 
                                var.equal = TRUE)$p.value,
    pval_TrustCentGov = t.test(TrustCentGov_new~ TreatInsc, 
                               var.equal = TRUE)$p.value,
    pval_TrustProvGov = t.test(TrustProvGov_new~ TreatInsc, 
                               var.equal = TRUE)$p.value,
    pval_int = t.test(IntForces_new~ TreatInsc, 
                      var.equal = TRUE)$p.value,
  )


control <- as.vector(t(si_table5[1,-c(1,18)]))
treatment <- as.vector(t(si_table5[2,-c(1,18)]))


pval_five <- as.vector(unlist(c(pvl5[1], NA, pvl5[2], NA, pvl5[3], NA, 
                                       pvl5[4], NA, pvl5[5], NA, pvl5[6], NA,
                                       pvl5[7], NA, pvl5[8], NA
)))


b_vars <- c(
  "Security Support", "",
  "Military Experience", "",
  "Mujahideen Experience", "",
  "Religiosity", "",
  "Punish Crime", "",
  "National Gov Support", "",
  "Prov Gov Support", "",
  "Int Support", "",
  "Respondents")


counts <- si_table5$count

cb_tab <- data.frame(matrix(nrow = 17, ncol = 4))

cb_tab[,1] <- c(b_vars)
cb_tab[,2] <- c(control, counts[1])
cb_tab[,3] <- c(treatment, counts[2])
cb_tab[,4] <- c(pval_five, NA)


# Round all numbers to 3 digits
cb_tab <- cb_tab %>%
  purrr::modify_if(~is.numeric(.), ~round(., 4))


# Add descriptive row at top
si_tab5 <- data.frame(rbind(c("", "Mean \n (SD)", "Mean \n (SD)", 
                                        "P Value"),
                                      cb_tab))

# Add column names
names(si_tab5) <- c("", "Neutral Prime", "Insecurity Prime", "")

View(si_tab5) # SI Table 5 



# SI Table 6: Mechanisms: Male vs Female (Treated Respondents)  (SI 2.2)


si_table6 <- balance[balance$TreatInsc == 1,] %>%
  group_by(female) %>%
  summarize(
    avg_manipulation = mean(manipulation),
    sd_manipulation = sd(manipulation),
    avg_behave_peace = mean(Behav_Peace_new),
    sd_behave_peace = sd(Behav_Peace_new),
    avg_military = mean(Afg_Mil_new),
    sd_military = sd(Afg_Mil_new),
    avg_muj = mean(Afg_Muj_new),
    sd_muj = sd(Afg_Muj_new),
    avg_religion = mean(Religious_new),
    sd_religion = sd(Religious_new),
    avg_punish = mean(Behav_law_punish_new),
    sd_punish = sd(Behav_law_punish_new),
    avg_TrustCentGovs = mean(TrustCentGov_new),
    sd_TrustCentGov = sd(TrustCentGov_new),
    avg_TrustProvGov = mean(TrustProvGov_new),
    sd_TrustProvGov = sd(TrustProvGov_new),
    avg_int = mean(IntForces_new),
    sd_int = sd(IntForces_new),
    count = n())


pvl6 <- balance[balance$TreatInsc == 1,] %>%
  summarise(
    pval_manipulation = t.test(manipulation ~ female, 
                               var.equal = TRUE)$p.value,
    pval_behave_peace = t.test(Behav_Peace_new~ female, 
                               var.equal = TRUE)$p.value,
    pval_military = t.test(Afg_Mil_new ~ female,
                           var.equal = TRUE)$p.value,
    pval_muj = t.test(Afg_Muj_new ~ female, 
                      var.equal = TRUE)$p.value,
    pval_religion = t.test(Religious_new~ female, 
                           var.equal = TRUE)$p.value,
    pval_behave_punish = t.test(Behav_law_punish_new~ female, 
                                var.equal = TRUE)$p.value,
    pval_TrustCentGov = t.test(TrustCentGov_new~ female, 
                               var.equal = TRUE)$p.value,
    pval_TrustProvGov = t.test(TrustProvGov_new~ female, 
                               var.equal = TRUE)$p.value,
    pval_int = t.test(IntForces_new~ female, 
                      var.equal = TRUE)$p.value,)


control <- as.vector(t(si_table6[1,-c(1,20)]))
treatment <- as.vector(t(si_table6[2,-c(1,20)]))


pval_six <- as.vector(unlist(c(pvl6[1], NA, pvl6[2], NA, pvl6[3], NA, 
                                 pvl6[4], NA, pvl6[5], NA, pvl6[6], NA,
                                 pvl6[7], NA, pvl6[8], NA, pvl6[9], NA)))


b_vars <- c(
  "Manipulation", "",
  "Security Support", "",
  "Military", "",
  "Mujahadeen", "",
  "Pious", "",
  "Punish", "",
  "Trust National Gov", "",
  "Trust Prov Gov", "",
  "International Forces Support", "",
  "Respondents")


counts <- si_table6$count

cb_tab <- data.frame(matrix(nrow = 19, ncol = 4))

cb_tab[,1] <- c(b_vars)
cb_tab[,2] <- c(control, counts[1])
cb_tab[,3] <- c(treatment, counts[2])
cb_tab[,4] <- c(pval_six, NA)


# Round all numbers to 3 digits
cb_tab <- cb_tab %>%
  purrr::modify_if(~is.numeric(.), ~round(., 4))


# Add descriptive row at top
si_tab6 <- data.frame(rbind(c("", "Mean \n (SD)", "Mean \n (SD)", 
                              "P Value"),
                            cb_tab))

# Add column names
names(si_tab6) <- c("", "Insecurity Prime (Male)", "Insecurity Prime (Female)", "")

View(si_tab6) # SI Table 6







